const p1 =  Promise.resolve('p1'); // 返回一个立刻被解决的prommise实例
const p2 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('p2延迟1秒')
    }, 1000)
})
const p3 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('p3延迟2秒')
    }, 2000)
})
// const p4 = Promise.reject('p4 rejected')
const p5 = new Promise((resolve, reject) => {
    setTimeout(() => {
        reject('p5 rejected 延迟1.5秒')
        console.log('p5')
    }, 1500)
})


Promise.MyAll = (promises) => {
    let arr = [], // 和promise顺序一样
    count = 0;
    return new Promise((resolve, reject) => {
        // 并发
        promises.forEach((item, i) => {
            // 函数作用域 i VO Value Object
            Promise.resolve(item).then(res => {
                arr[i] = res
                count += 1
                if (count === promises.length) resolve(arr)
            }, reject)
        })
    })
}

Promise.MyAll([p1, p2, p3, p5])
    .then(res => console.log(res))
    .catch(err => console.log(err))